'use strict';
(function() {
	var search = (function($) {
		var
		searchForm = {
			init : function() {
				// helper.date_ymd(true);
				// helper.date_his();
				this.excelChoose();
			},
			excelChoose : function() {
				var index = 0;
				$('#chooseButton').click(function() {
					index = layer.open({
					  type: 1,
					  title: '请选择要查询的表格类型：',
					  skin: 'layui-layer-rim', //加上边框
					  area: ['420px', '350px'], //宽高
					  content: '<div class="sample-list">' + 
			  					'<a class="btn btn-success" href="#" id="pdButton">1. PD</a>' + 
			  					'<a class="btn btn-success" href="/search/search/excel/?type=et">2. ET</a>' +
			  					'<a class="btn btn-success" href="/search/search/excel/?type=sca">3. SCA</a>' +
			  					'<a class="btn btn-success" href="/search/search/excel/?type=cmt">4. CMT</a>' +
			  					'<a class="btn btn-success" href="/search/search/excel/?type=hsp">5. HSP</a>' +
			  					'<a class="btn btn-success" href="/search/search/excel/?type=pfbc">6. PFBC</a>' +
			  					'<a class="btn btn-success" href="/search/search/excel/?type=fcmte">7. FCMTE</a></div>',
					});
					$('#pdButton').click(function() {
						layer.close(index);
						layer.open({
						  type: 1,
						  title: '请选择要查询的表格类型：',
						  skin: 'layui-layer-rim', //加上边框
						  area: ['420px', '260px'], //宽高
						  content: '<div class="sample-list">' + 
				  					'<a class="btn btn-success" href="/search/search/excel/?type=pd">PD初诊</a>' +
				  					'<a class="btn btn-success" href="/search/search/excel/?type=pdf">PD复诊</a>' +
				  					'<a class="btn btn-success" href="/search/search/excel/?type=udprs">美多芭冲击试验</a>' +
				  					'<a class="btn btn-success" href="/search/search/excel/?type=udprs2">UDPRS</a></div>',
						});
						return false;
					});
					return false;
				});
			},
		},
		dropDown = {
			addToHiddenFlag : false,
			init : function() {
				this.show();
				this.click();
				this.initInput($("#searchsenior-pt_history"));
				this.initInput($("#searchsenior-pt_label"));
				this.addToHiddenFlag = true;
			},
			initInput : function(input) {
				var ids = $(input).val(),
					ids = (typeof(ids) != 'undefined') ? ids.split("+") : [],
					list = $(input).parent().siblings(".dropdown-list");

				for (var i in ids) {
					if (ids[i] == "") continue;
					$(list).find("a[data-id=" + ids[i] + "]").click();
				}
			},
			show : function() {
				$(".dropdown-toggle").on('click', function(event) {
					if ($(this).hasClass('open')) {
						$(this).siblings('.dropdown-list').css('display', 'none');
						$(this).removeClass('open');
						return ;
					}
					$(this).siblings('.dropdown-list').css('display', 'block');
					$(this).addClass('open');

					event.stopPropagation();
				});

				$(document).click(function(event) {//click space hide
					var toggle = $(".dropdown-toggle"),
						list = $(toggle).siblings(".dropdown-list");

					if(!toggle.is(event.target) && toggle.has(event.target).length === 0 && !list.is(event.target) && list.has(event.target).length === 0) {
						$(toggle).removeClass('open');
						$(list).css('display', 'none');
					}
				});
			},
			cancel : function() {
				$(".label.label-info").find("i").unbind('click').bind('click', function(event) {
					var _value = $(this).parent().attr("data-id");
					$(this).parent().parent().siblings(".dropdown-list").find("a[data-id=" + _value + "]").click();

					event.stopPropagation();
				});
			},
			click : function() {
				var self = this;

				$("span", ".dropdown-list").click(function() {
					var _a = $("a", this),
						_value = $(_a).attr("data-id");

					if ($(_a).hasClass("unselected")) {
						(self.addToHiddenFlag) ? hidden.choose(this, _value) : '';
						toggle.choose(this, _value);
						list.choose(_a);
						self.cancel();
					} else {
						hidden.cancel(this, _value);
						toggle.cancel(this, _value);
						list.cancel(_a);
					}
				});
			},
		},
		hidden = {
			choose : function(btn, _value) {
				var input = $(btn).parent().siblings("div").find("input"),
					origin_value = $(input).val();

				$(input).val(origin_value + "+" + _value);
			},
			cancel : function(btn, _value) {
				var input = $(btn).parent().siblings("div").find("input"),
					str_pattern = '([^0-9]+|^)' + _value + '([^0-9]+|$)',
	                pattern = new RegExp(str_pattern),
	                after = input.val().replace(pattern, '+');

	            $(input).val(after);
			}
		},
		list = {
			choose : function(_a) {
				$(_a).removeClass("unselected");
				$(_a).addClass("selected");
			},
			cancel : function(_a) {
				$(_a).removeClass("selected");
				$(_a).addClass("unselected");
			}
		},
		toggle = {
			choose : function(btn, _value) {
				var _toggle = $(btn).parent().siblings(".dropdown-toggle"),
					html = $(_toggle).html();
					
				html = html + '<span class="label label-info" data-id="' + _value + '">' + $(btn).text() + '<i class="fa fa-times"></i></span>';
				$(_toggle).html(html);
			},
			cancel : function(btn, _value) {
				var label = $(btn).parent().siblings("span").find("span[data-id=" + _value + "]");
				$(label).remove();
			}
		},
		init = function() {
			searchForm.init();
			dropDown.init();
		};
		return {
			init : init
		};
	})(jQuery);

	search.init();
})();